package com.vp.org.dao.impl;

import com.vp.org.constant.Sql;
import com.vp.org.dao.OrdersDao;
import com.vp.org.pojo.AdminInfo;
import com.vp.org.pojo.Orders;
import com.vp.org.util.JdbcUtil;

import java.util.List;
import java.util.Map;
import java.util.zip.CheckedOutputStream;

public class OrdersDaoImpl extends JdbcUtil implements Sql, OrdersDao {

    // 后台订单总数
    // select count('id') count from orders
    @Override
    public long ordersPageCount() {
        return (long) template.queryForMap(ORDERS_ORDERSPAGECOUNT).get("count");
    }

    // 总分页数
    @Override
    public int ordersPage() {
        int count = (int) ordersPageCount();
        return count % 4 == 0 ? count / 4 : count / 4 + 1;
    }

    // 后台订单分页查询
    // select o.*,u.address from user_info u,orders o,book_info b where  b.admin='admin' and u.user_id=o.user_id limit 0,4;
    @Override
    public List<Map<String, Object>> ordersPageQuery(AdminInfo adminInfo, int page) {
        return template.queryForList(ORDERS_ORDERSPAGEQUERY,adminInfo.getAdmin(), (page -1) * 4);
    }

    // 后台删除订单
    // delete from orders where id = ?;
    @Override
    public int deleteOrders(Orders orders) {
        return template.update(ORDERS_DELETE,orders.getId());
    }

    // 销售额（商品总价格）
    // select sum(price * count) from orders;
    @Override
    public double saleOrders() {
        return (double)template.queryForMap(ORDERS_SALE).get("sum");
    }
}
